package gui;

import util.exceptions.FOCException;
import util.exceptions.UnbalancableEquationException;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.util.Scanner;

public class ValidateBuild {
    static MainGui gui;

    public static void main(String args[]) throws Exception {
        BufferedInputStream equationFile;
        try {
            equationFile = new BufferedInputStream(new FileInputStream(new File("trunk//gui//equations.csv")));
        } catch (Exception e) {
            equationFile = new BufferedInputStream(new FileInputStream(new File("src//gui//equations.csv")));
        }
        Scanner in = new Scanner(equationFile);
        MainGui.setLookandFeel();
        MainGui.loadTable();
        gui = new MainGui();

        while (in.hasNext()) {
            String input[] = in.nextLine().split(",");
            String equation = input[0];
            String solution = input[1];
            if (getSolution(equation).replace(" ", "").equalsIgnoreCase(solution.replace(" ", ""))) {
                System.out.println("Test Phase Passed: " + equation + " == " + solution);
            } else {
                System.err.println("Test Failed!");
                System.err.println("equation != solution");
            }
        }
    }

    private static String getSolution(String equation) {
        String[] LandR = equation.split("=");
        try {
            gui.setEquation(LandR[0], LandR[1]);
            gui.equation.balance();
        } catch (UnbalancableEquationException e1) {
            e1.showMessageDialog();
        } catch (FOCException e) {
            e.printStackTrace();
        }
        return gui.equation.toString();
    }
}
